<?php
/**
 * Created by PhpStorm.
 * User: xaq
 * Date: 2017/7/10
 * Time: 10:41
 */

namespace app\index\controller;

use app\index\service\WxResponse;

class Menu extends BaseController
{
    protected $access_token;
    public function set(){
        $menu = array(
            "button" => [
                [
                    "name" => "教师登陆",
                    "type" => "view",
                    "url" => "http://callname.tunnel.echomod.cn/index/User/index"
                ],
                [
                    "name" => "点名",
                    "type" => "view",
                    "url" => "http://callname.tunnel.echomod.cn/index/oauth/login/snsapi_userinfo"
                ],
                [
                    "name" => "考勤查询",
                    "type" => "view",
                    "url" => "https://www.xaqtj.com"
                ]
            ]
        );
        $token = new Token();
        $this->access_token = $token->getAccessToken();
        $path = sprintf(config('wx.menu_token_url'),$this->access_token);
        //另一种方法urlencode()和urldecode()
        $data = json_encode($menu,JSON_UNESCAPED_UNICODE);
        $result = Token::send($path,$data,'POST');
        return $result;
    }

    /**
     * 接收事件推送并回复
     */
    public function responseMsg(){

        //1、获取到微信推送过来post数据（xml格式）
        $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
        //2、处理消息类型，并设置回复类型和内容
        $postObj = simplexml_load_string($postArr);

        //如果是自定义菜单中的event -> click
        if(strtolower($postObj->Event) == 'click'){
            if(strtolower($postObj->EventKey) == 'callname'){
                $content = "这是callname的事件推送";
            }
        }
        $wxResponse = new WxResponse();
        $wxResponse->responseText($postObj,$content='');
    }

}